﻿using LBSoft.Common.DotNetConfig;
using LBSoft.Common.DotNetExt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LBSoft.HplusAdmin.UserControl
{
    public partial class HeadInclude : System.Web.UI.UserControl
    {
        private string SiteCssJsFormat = ConfigHelper.GetAppSettings("SiteCssJsFormat").ToString2();
        private string BaiduMapAK = ConfigHelper.GetAppSettings("BaiduMapAK").ToString2();

        /// <summary>
        /// 当前时间格式-禁用浏览器css、js脚本缓存
        /// 开发：yyyyMMddHHmmss 正式：yyyyMMddHH
        /// </summary>
        public string vStr = DateTime.Now.ToString("yyyyMMddHH");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(SiteCssJsFormat))
            {
                vStr = DateTime.Now.ToString(SiteCssJsFormat);
            }
            Literal lcInclude = new Literal();
            lcInclude.Text = this.GetIncludeCode();
            this.Controls.Add(lcInclude);
        }

        #region 只读属性
        /// <summary>
        /// Script引入的格式化串，格式为"&lt;script src='{resourceUrl}{0}' type='text/javascript' language='javascript'&gt;&lt;/script&gt;"，{resourceUrl}表示资源文件的站点地址，{0}表示文件路径
        /// </summary>
        protected string ScriptFormat
        {
            get { return this.scriptFormat; }
        }

        /// <summary>
        /// CSS引入的格式化串，格式为"&lt;link href='{resourceUrl}{0} ' rel='stylesheet' type='text/css' &gt;&lt;/link&gt;"，{resourceUrl}表示资源文件的站点地址，{0}表示文件路径
        /// </summary>
        protected string CSSFormat
        {
            get { return this.cssFormat; }
        }
        #endregion

        #region 可读可写属性

        //------------------------------其他插件类------------------------------
        public bool HasBootstrap
        {
            set { this.hasBootstrap = value; }
            get { return this.hasBootstrap; }
        }

        public bool HasBootstrapTable
        {
            set { this.hasBootstrapTable = value; }
            get { return this.hasBootstrapTable; }
        }

        public bool HasTreeGrid
        {
            set { this.hasTreeGrid = value; }
            get { return this.hasTreeGrid; }
        }

        public bool HasFixedColumns
        {
            set { this.hasFixedColumns = value; }
            get { return this.hasFixedColumns; }
        }

        public bool HasLayui
        {
            set { this.hasLayui = value; }
            get { return this.hasLayui; }
        }

        public bool HasLayer
        {
            set { this.hasLayer = value; }
            get { return this.hasLayer; }
        }

        public bool HasCommonUpload
        {
            set { this.hasCommonUpload = value; }
            get { return this.hasCommonUpload; }
        }


        public bool HasBaiduMap
        {
            set { this.hasBaiduMap = value; }
            get { return this.hasBaiduMap; }
        }

        public bool HasWebcam
        {
            set { this.hasWebcam = value; }
            get { return this.hasWebcam; }
        }

        public bool HasCookie
        {
            set { this.hasCookie = value; }
            get { return this.hasCookie; }
        }

        public bool HasPageSelect
        {
            set { this.hasPageSelect = value; }
            get { return this.hasPageSelect; }
        }

        public bool HasMoment
        {
            set { this.hasMoment = value; }
            get { return this.hasMoment; }
        }

        public bool HasKindeditor
        {
            set { this.hasKindeditor = value; }
            get { return this.hasKindeditor; }
        }

        public bool HasHtmlUtil
        {
            set { this.hasHtmlUtil = value; }
            get { return this.hasHtmlUtil; }
        }

        public bool HasClipboard
        {
            set { this.hasClipboard = value; }
            get { return this.hasClipboard; }
        }

        public bool HasTaffydb
        {
            set { this.hasTaffydb = value; }
            get { return this.hasTaffydb; }
        }

        public bool HasCustomScrollbar
        {
            set { this.hasCustomScrollbar = value; }
            get { return this.hasCustomScrollbar; }
        }

        //------------------------------其他样式类------------------------------
        public bool HasIconfont
        {
            set { this.hasIconfont = value; }
            get { return this.hasIconfont; }
        }

        public bool HasMainForm
        {
            set { this.hasMainForm = value; }
            get { return this.hasMainForm; }
        }

        public bool HasMainStyle
        {
            set { this.hasMainStyle = value; }
            get { return this.hasMainStyle; }
        }

        public bool HasFontAwesomeStyle
        {
            set { this.hasFontAwesomeStyle = value; }
            get { return this.hasFontAwesomeStyle; }
        }
        #endregion

        #region 私有变量
        private string scriptFormat = "<script src='{0}' type='text/javascript' language='javascript'></script>\r\n";
        private string cssFormat = "<link href='{0}' rel='stylesheet' type='text/css' />\r\n";

        //------------------------------其他插件类------------------------------
        private bool hasBootstrap = false;
        private bool hasBootstrapTable = false;
        private bool hasTreeGrid = false;
        private bool hasFixedColumns = false;
        private bool hasLayui = false;
        private bool hasLayer = false;
        private bool hasCommonUpload = false;
        private bool hasBaiduMap = false;
        private bool hasWebcam = false;
        private bool hasCookie = false;
        private bool hasPageSelect = false;
        private bool hasMoment = false;
        private bool hasKindeditor = false;
        private bool hasHtmlUtil = false;
        private bool hasClipboard = false;
        private bool hasTaffydb = false;
        private bool hasCustomScrollbar = false;

        //------------------------------其他样式类------------------------------
        private bool hasMainStyle = false;
        private bool hasMainForm = false;
        private bool hasIconfont = false;
        private bool hasFontAwesomeStyle = false;
        #endregion

        #region 私有方法
        /// <summary>
        /// 获取引入文件的代码
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns></returns>
        private string GetIncludeCode()
        {
            StringBuilder code = new StringBuilder();

            //------------------------------基础共用类------------------------------

            code.AppendFormat(scriptFormat, "/resources/js/jquery.min.js");
            code.AppendFormat(scriptFormat, "/resources/js/json2.js");
            code.AppendFormat(scriptFormat, "/resources/js/app.js?v=" + vStr);

            //------------------------------其他插件类------------------------------

            //加载bootstrap
            if (hasBootstrap)
            {
                code.AppendFormat(cssFormat, "/resources/bootstrap-3.3.7/css/bootstrap.min.css");
                code.AppendFormat(scriptFormat, "/resources/bootstrap-3.3.7/js/bootstrap.min.js");
            }

            //加载bootstrap-table
            if (hasBootstrapTable)
            {
                code.AppendFormat(cssFormat, "/resources/bootstrap-table/css/bootstrap-table.css");
                code.AppendFormat(scriptFormat, "/resources/bootstrap-table/js/bootstrap-table.js");
                code.AppendFormat(scriptFormat, "/resources/bootstrap-table/js/bootstrap-table-zh-CN.js");
            }

            //加载treegrid
            if (hasTreeGrid)
            {
                code.AppendFormat(cssFormat, "/resources/bootstrap-table-treegrid/jquery.treegrid.min.css");
                code.AppendFormat(scriptFormat, "/resources/bootstrap-table-treegrid/bootstrap-table-treegrid.js");
                code.AppendFormat(scriptFormat, "/resources/bootstrap-table-treegrid/jquery.treegrid.min.js");
            }

            //加载fixed-columns 这里和hasBootstrapTable bootstrap-table版本不一样，不要同时使用
            if (hasFixedColumns)
            {
                code.AppendFormat(cssFormat, "/resources/bootstrap-table-fixed/bootstrap-table.css");
                code.AppendFormat(scriptFormat, "/resources/bootstrap-table-fixed/js/bootstrap-table.js");
                code.AppendFormat(scriptFormat, "/resources/bootstrap-table-fixed/bootstrap-table-zh-CN.js");
                code.AppendFormat(cssFormat, "/resources/bootstrap-table-fixed/bootstrap-table-fixed-columns.min.css");
                code.AppendFormat(scriptFormat, "/resources/bootstrap-table-fixed/bootstrap-table-fixed-columns.min.js");
            }

            //加载layui  
            if (hasLayui)
            {
                code.AppendFormat(cssFormat, "/resources/layui/css/layui.css");
                code.AppendFormat(cssFormat, "/resources/css/layui-public.css");
                code.AppendFormat(scriptFormat, "/resources/layui/layui.js");
            }

            //加载layer 
            if (hasLayer)
            {
                code.AppendFormat(scriptFormat, "/resources/layer/layer.js");
            }

            //加载commonUpload
            if (hasCommonUpload)
            {
                code.AppendFormat(cssFormat, "/resources/css/commonUpload.css");
                code.AppendFormat(scriptFormat, "/resources/js/commonUpload.js");
            }

            //加载BaiduMap 
            if (hasBaiduMap)
            {
                code.AppendFormat(scriptFormat, $"https://api.map.baidu.com/api?&v=2.0&ak={BaiduMapAK}");
            }

            //加载Webcam
            if (hasWebcam)
            {
                code.AppendFormat(scriptFormat, "/resources/jquery-webcam/jquery.webcam.js");
            }
            //加载Cookie
            if (hasCookie)
            {
                code.AppendFormat(scriptFormat, "/resources/jquery-cookie/jquery.cookie-1.4.1.min.js");
            }

            //加载分页下拉
            if (hasPageSelect)
            {
                code.AppendFormat(cssFormat, "/resources/selectpage/selectpage.css");
                code.AppendFormat(scriptFormat, "/resources/selectpage/selectpage.js");
            }

            //加载时间控件
            if (hasMoment)
            {
                code.AppendFormat(scriptFormat, "/resources/moment/moment.min.js");
            }

            //加载Kindeditor编辑器
            if (hasKindeditor)
            {
                code.AppendFormat(scriptFormat, "/resources/kindeditor/kindeditor-all-min.js");
                code.AppendFormat(scriptFormat, "/resources/kindeditor/lang/zh-CN.js");
            }

            //加载HtmlUtil
            if (hasHtmlUtil)
            {
                code.AppendFormat(scriptFormat, "/resources/js/HtmlUtil.js");
            }

            //加载Clipboard
            if (hasClipboard)
            {
                code.AppendFormat(scriptFormat, "/resources/clipboard/clipboard.min.js");
            }

            //加载Taffydb
            if (hasTaffydb)
            {
                code.AppendFormat(scriptFormat, "/resources/taffydb/taffy-min.js");
            }

            //加载滚动条
            if (hasCustomScrollbar)
            {
                code.AppendFormat(cssFormat, "/resources/mCustomScrollbar/jquery.mCustomScrollbar.css");
                code.AppendFormat(scriptFormat, "/resources/mCustomScrollbar/jquery.mCustomScrollbar.min.js");
            }

            //------------------------------其他样式类------------------------------

            //加载main样式
            if (hasMainStyle)
            {
                code.AppendFormat(cssFormat, "/resources/css/main.css?v=" + vStr);
            }
            //加载MainForm 
            if (hasMainForm)
            {
                code.AppendFormat(cssFormat, "/resources/css/MainForm.css?v=" + vStr);
            }

            //加载Iconfont 
            if (hasIconfont)
            {
                code.AppendFormat(cssFormat, "/resources/font/iconfont.css?v=" + vStr);
            }

            //加载Font-Awesome样式
            if (hasFontAwesomeStyle)
            {
                code.AppendFormat(cssFormat, "/resources/css/font-awesome.min.css");
            }

            return code.ToString();
        }
        #endregion
    }
}